package com.cdut.hwadeefsc.controller;

import com.cdut.hwadeefsc.entity.organizations;
import com.cdut.hwadeefsc.service.organizationsService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/orgs")
public class organizationsController {

    @Resource
    private organizationsService organizationsService;

    /**
     * 获取班级的结构数据
     */
    @GetMapping("/get")
    public ResponseEntity<?> getOrganizations() {
        try {
            List<organizations> organizations = organizationsService.get();
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", organizations);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", "获取组织机构失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }

    /**
     * 创建组织机构（班级）
     */
    @PostMapping("/create")
    public ResponseEntity<?> createOrganization(@RequestBody organizations organization) {
        try {

            int result = organizationsService.create(organization);

            Map<String, Object> response = new HashMap<>();
            if (result > 0) {
                response.put("success", true);
                response.put("message", "组织机构创建成功");
                response.put("orgId", organization.getId());
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "组织机构创建失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
            }
        } catch (Exception e) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", "创建组织机构时发生错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }

    /**
     * 修改组织机构信息（如冻结毕业班级）
     */
    @PutMapping("/modify")
    public ResponseEntity<?> modifyOrganization(
            @RequestParam Integer orgId,
            @RequestBody organizations organization) {
        try {
            // 验证参数
            if (orgId == null || orgId <= 0) {
                Map<String, Object> error = new HashMap<>();
                error.put("success", false);
                error.put("message", "无效的组织机构ID");
                return ResponseEntity.badRequest().body(error);
            }

            organizations tempOrg =  organizationsService.getById(orgId);

            // 设置要更新的ID和更新时间
            organization.setId(Long.valueOf(orgId));
            organization.setName(organization.getName() == null ? tempOrg.getName() : organization.getName());
            organization.setType(organization.getType() == null ? tempOrg.getType() : organization.getType());
            organization.setStatus(organization.getStatus() == null ? tempOrg.getStatus() : organization.getStatus());
            organization.setParentId(organization.getParentId() == null ? tempOrg.getParentId() : organization.getParentId());

            int result = organizationsService.modify(organization);

            Map<String, Object> response = new HashMap<>();
            if (result > 0) {
                response.put("success", true);
                response.put("message", "组织机构更新成功");
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "组织机构更新失败，可能不存在该组织机构");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }
        } catch (Exception e) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", "更新组织机构时发生错误: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }
}
